package com.cxs.service.impl;

import com.cxs.mapper.AirQualityIndexMapper;
import com.cxs.model.AirQualityIndex;
import com.cxs.service.AirQualityIndexService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cxs.utils.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/*
 * @Author:cxs
 * @Date:2021/9/30  14:20
 * @Version 1.0
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
@Service
public class AirQualityIndexServiceImpl implements AirQualityIndexService {
    @Autowired
    private AirQualityIndexMapper airQualityIndexMapper;
    @Override
    public PageInfo<AirQualityIndex> listAllByPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<AirQualityIndex> qualityIndexList = airQualityIndexMapper.selectAllunionDistrict();
        PageInfo<AirQualityIndex> page = new PageInfo<>(qualityIndexList);
        return page;
    }

    @Override
    public AirQualityIndex getOneById(Integer id) {
        AirQualityIndex airQualityIndex = airQualityIndexMapper.selectByPrimaryKey(id);
        airQualityIndex.setTime1(airQualityIndex.getMonitorTime().getTime());
        airQualityIndex.setTime2(airQualityIndex.getLastModifityTime().getTime());
        return airQualityIndex;
    }

    @Override
    public Result update(AirQualityIndex obj) {
        if(obj.getId() == null){
            return Result.failare("修改失败");
        }
        return airQualityIndexMapper.updateByPrimaryKeySelective(obj) > 0 ? Result.OK("修改成功") : Result.failare("修改失败");
    }

    @Override
    public Result delete(Integer id) {
        return airQualityIndexMapper.deleteByPrimaryKey(id) > 0 ? Result.OK("删除成功") : Result.failare("删除失败");
    }

    @Override
    public Result add(AirQualityIndex obj) {
        return airQualityIndexMapper.insertSelective(obj) > 0 ? Result.OK("添加成功") : Result.failare("添加失败");
    }

    @Override
    public PageInfo<AirQualityIndex> search(Integer id,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<AirQualityIndex> qualityIndexList = airQualityIndexMapper.selectAllunionDistrictBydisId(id);
        PageInfo<AirQualityIndex> page = new PageInfo<>(qualityIndexList);
        return page;
    }
}
